﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace ic_card_emulator
{
    /// <summary>
    /// HTTPクライアント
    /// 
    /// ADAPTERパターンだが敢えてインターフェイスを少なくする
    /// 継承ではなくて委譲を用いる
    /// 
    /// こっち側に使うインターフェイスを書いていく
    /// </summary>
    class HttpClient 
    {
        WebClientEx client = new WebClientEx();

    }

    /// <summary>
    /// クッキーの為だけに継承
    /// </summary>
    class WebClientEx : WebClient
    {
        private CookieContainer cookieContainer;

        public CookieContainer CookieContainer
        {
            get
            {
                return cookieContainer;
            }
            set
            {
                cookieContainer = value;
            }
        }

        protected override WebRequest GetWebRequest(Uri uri)
        {
            WebRequest webRequest = base.GetWebRequest(uri);

            if (webRequest is HttpWebRequest)
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)webRequest;
                httpWebRequest.CookieContainer = this.cookieContainer;
            }

            return webRequest;
        }
    }
}
